package by.vr.jpa.result.transformer.util;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * 
 * @author vrimashevsky
 *
 */
public class PropertyUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);
	
	public static final String DOT_REGEX = "\\.";
	
	public static final String DOT = ".";
	
	public static final String GET = "get";
	public static final String IS = "is";
	public static final String SET = "set";

	public static String[] getProperties(final String propertyName){
		return propertyName.split(DOT_REGEX);
	}
	
	public static String getProperty(final String properties[], final int first){
		final int propertiesSize = properties.length;
		final StringBuilder sb = new StringBuilder();
		for(int i = first; i < propertiesSize; i++){
			sb.append(properties[i]);
			if(i != propertiesSize -1){
				sb.append(DOT);
			}
		}
		return sb.toString();
	}
	
	
	public static Collection initCollection(final Class collectionClass){
		final Collection collection;
		if(Set.class.isAssignableFrom(collectionClass)){
			collection = Sets.newHashSet();
		}else if(List.class.isAssignableFrom(collectionClass)){
			collection = Lists.newArrayList();
		}else{
			throw new RuntimeException("Unknown collection: " + collectionClass);
		}
		return collection;
	}
	
	public static  boolean isGetter(String methodName){
		if(methodName.startsWith(GET) || methodName.startsWith(IS)){
			return true;
		}else{
			return false;
		}
	}
	
	
	public static boolean isSetter(String methodName){
		if(methodName.startsWith(SET)){
			return true;
		}else{
			return false;
		}
	}
	
	public static String getPropertyName(String methodName){
		final String propertyName = by.vr.jpa.result.transformer.util.StringUtils.removeIfStartsWith(methodName, GET, SET, IS);
		return StringUtils.uncapitalize(propertyName);
	}


}
